(function() {

    var
    $u = juliet.using({
        Observer : 'juliet.core.Observer-0.1',
        PopupList : 'juliet.ui.popup.control.PopupList-0.1'
    }),
    $ = juliet.jQuery;

    var CustomInputWithPopupListClass = function() {

        /**
         * Instance options (see inherited classes for more details). Contains:
         *     - {String} selector
         *     - {Function} formatTitle
         *
         * @type Object
         * @private
         */
        this._options = null;

        /**
         * Current selected item. Object with "title" and "value" fields.
         *
         * @type Object
         * @private
         */
        this._selectedItem = null;

        /**
         * Instance observer.
         *
         * @see ObserverClass
         * @type Object
         * @private
         */
        this._observer = null;

        /**
         * Popup list with possible values.
         *
         * @see PopupListClass
         * @type Object
         * @private
         */
        this._popupList = null;

        /**
         * @type JQueryElement
         * @private
         */
        this._controlElement = null;

        /**
         * Is input active?
         *
         * @type Boolean
         * @private
         */
        this._active = true;

        // -------------------------------------------------------
        // -                        LOGIC                        -
        // -------------------------------------------------------

        /**
         * @private
         */
        this._initializePopupList = function() {
            this._popupList = juliet.createObject($u('PopupList'), {
                ownerElement : this._controlElement,
                formatTitle : this._options.formatTitle
            });
        };

        //
        // API
        //

        this.enable = function() {
            if (this._active) return;

            this._active = true;
            this._controlElement.removeClass('disabled');
            this._observer.notify('enabled');
        };

        this.disable = function() {
            if (!this._active) return;

            this._active = false;
            this._controlElement.addClass('disabled');
            this.setSelectedItem(null);
            this._observer.notify('disabled');
        };

        /**
         * @param {Object} item Object with "title" and "value" fields.
         */
        this.setSelectedItem = function(item) {
            this._selectedItem = item;
            this._observer.notify('item-selected', item);
        };

        /**
         * @param {Object} o Instance options (see "_options" field for more details).
         */
        this.initialize = function(o) {
            this._options = o;

            this._controlElement = $(o.selector);
            this._observer = juliet.createObject($u('Observer'));

            this._initializePopupList();
        };

        // -------------------------------------------------------
        // -                      GET & SET                      -
        // -------------------------------------------------------

        this.getObserver = function() {
            return this._observer;
        };

        this.getSelectedItem = function() {
            return this._selectedItem;
        };

        this.getPopupList = function() {
            return this._popupList;
        };

    };

    juliet.registerModule(
        'juliet.ui.input.withPopupList', 'CustomInputWithPopupList-0.1',
        CustomInputWithPopupListClass
    );

})();
